package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private DoGoodMapper doGoodMapper;
    @Autowired
    private SystemNoticeMapper noticeMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        qo.setType(2);
        List<Map> maps = commentMapper.selectTravelCommentForCount(qo);
        for (Travel travel : list) {
            //查询该条游记的点赞数
            int doGoogNum = doGoodMapper.selectForTravelCount(travel.getId());
            travel.setDoGoodNum(doGoogNum);

            Long id = travel.getId();
            for (Map map : maps) {
                if(map.get("cid")==id){
                    travel.setTarvelCommentNum((Long) map.get("count"));
                }
            }
        }
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        if (travel.getId() == null) {
            travel.setState(0);
            //设置作者
            travel.setAuthor(UserContext.getCurrentUser());
            //保存游记基本信息
            travelMapper.insert(travel);
            //保存游记内容
            TravelContent content = travel.getTravelContent();
            //主键关联
            content.setId(travel.getId());
            travelContentMapper.insert(content);
        } else {
            //更新游记表信息
            travelMapper.updateByPrimaryKey(travel);
            //更新游记内容表
            TravelContent travelContent = travel.getTravelContent();
            //游记内容表的id和游记的id关联
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }
    }

    @Override
    public Travel getTravelById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeStatus(Travel travel) {
        if (travel.getState() == 2) {
            travel.setReleaseTime(new Date());
        }
        travelMapper.changeStatus(travel);


    }

    /**
     * 根据关键字查询该游记的总数
     *
     * @param keyword
     * @return
     */
    public int selectCountByKeyword(String keyword) {
        return travelMapper.selectCountByKeyword(keyword);
    }

    /**
     * 通过关键字查询游记
     *
     * @param qo
     * @return
     */
    public PageInfo searchQuery(SearchQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.selectByKeyword(qo);
        return new PageInfo(list);
    }

    /**
     * 查询用户收藏的游记
     *
     * @param qo
     * @return
     */
    public PageInfo selectForCollect(CollectQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.selectForCollect(qo);
        return new PageInfo(list);
    }

    /**
     * 获取该用户被点赞的未读游记
     * @param qo
     * @return
     */
    public PageInfo selectTravelForDoGood(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Map<String, Object>> list = travelMapper.selectTravelForDoGood(qo);
        return new PageInfo(list);
    }
}
